package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.example.dao.PasswdDao;
import com.example.dao.UserDao;
import com.example.domain.Passwd;
import com.example.domain.User;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.UserService;
import com.example.tool.DateTool;
import com.example.tool.PasswordTool;

/**
 * @author 袁小弘
 * @describe 用户服务实现层
 * @date 2019-12-31
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Autowired
    PasswdDao passwdDao;

    /**
     * @describe 查询所有用户信息
     */
    @Override
    public Result queryAll() {

        List<User> list = userDao.findAll();

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 判断用户是否存在
     */
    @Override
    public boolean isExist(String userName) {
        User user = getByName(userName);
        return null != user;
    }

    public User getByName(String username) {
        return userDao.findByUserName(username);
    }

    /**
     * @describe 根据用户编号查询用户信息
     */
    @Override
    public Result queryUserById(String admin_Id) {

        User user = userDao.findByUserId(Integer.parseInt(admin_Id));
        // System.out.println("====="+user.getRecordTime());
        // System.out.println(DigestUtils.md5DigestAsHex(user.getUserName().getBytes()));

        if (user != null) {
            // user.setRecordTime(DateTool.getTimestampToString(user.getRecordTime()));
            return ResultFactory.buildSuccessResult(user);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }

    }

    /**
     * @describe 根据用户编号删除用户信息
     */
    @Override
    public Result deleteUserById(String user_Id) {
        int row = userDao.deleteByUserId(Integer.parseInt(user_Id));
        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }

    }

    /**
     * @describe 添加用户信息
     */
    @Override
    public Result addUser(User user) {
        if (user != null) {
            if (user.getBirthday().toString().equals("")) {

            }
            user.setRecordTime(DateTool.getTimestamp());
        }

        User user1 = userDao.save(user);

        Passwd pass = passwdDao.save(PasswordTool.setPasswdInfo(user1.getUserId()));

        if (user1 != null && pass != null) {
            return ResultFactory.buildSuccessResult(user1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新用户信息
     */
    @Override
    public Result updateUser(User user) {
        user.setUpdateTime(DateTool.getTimestamp());
        User user1 = userDao.save(user);

        if (user1 != null) {
            return ResultFactory.buildSuccessResult(user1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新用户信息
     */
    @Override
    public Result loginsUser(User user) {

//		User andPassword = userDao.getByAccountAndPassword(user.getUserName(), user.getEmail());
//		if (andPassword!=null) {
//			return ResultFactory.buildSuccessResult(andPassword);
//		}else {
//			return ResultFactory.buildDefaultFailResult();
//		}
        Map<String, Object> map = userDao.getTwoByAccountAndPassword(user.getUserName(), user.getEmail());
        System.out.println(JSON.toJSONString(map));
        System.out.println(map.get("id"));
        System.out.println(map.get("account"));
        System.out.println(map.get("pwd"));
        return null;
    }

    /**
     * @describe 根据编号批量删除用户信息
     */
    @Override
    public Result deleteUserByIds(Map<String, String> userIds) {

        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (userIds != null) {
            String ids = userIds.get("userIds");
            String[] id = ids.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = userDao.deleteByUserIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据Excel批量添加用户信息
     */
    @Override
    public Result addUserByExcel(List<User> list) {
        List<User> list1 = null;
        List<Passwd> list2 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = userDao.saveAll(list);
            System.out.println("------------" + list1);
            List<Passwd> list3 = PasswordTool.setPasswdInfoByList(list1);
            System.out.println("+++++++++" + list3);
            list2 = passwdDao.saveAll(list3);
        }
        if (list1 != null && list2 != null) {

            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 用户信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<User> list = null;
        if (info[0].equals("userName")) {

            list = userDao.findByUserNameLike("%" + info[1] + "%");

        } else if (info[0].equals("nickName")) {

            list = userDao.findByNickNameLike("%" + info[1] + "%");

        } else if (info[0].equals("telephone")) {

            list = userDao.findByTelephoneLike("%" + info[1] + "%");

        } else if (info[0].equals("email")) {

            list = userDao.findByEmailLike("%" + info[1] + "%");

        }

        System.out.println(info.toString() + "like==========" + list);

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有用户账号和ID==>用于密码管理
     */
    @Override
    public Result queryAllUserNameAndId() {
        List<Map<String, Object>> list = userDao.findByUserNameAndUserId();

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

}
